package edu.cmu.javadb.plan;


import java.util.*;

import edu.cmu.javadb.plan.condition.Path;
import edu.cmu.javadb.plan.visitor.Visitor;


public class Projection extends PlanNode {
	
	private List<Path> columns = new ArrayList<Path>();
	private PlanNode child = null;
	
	public Projection() {
		
	}
	public Projection(PlanNode child, List<Path> columns) {
		this.child = child;
		this.columns = columns;
	}
	
	@Override
	public <T> T accept(Visitor<T> visitor) {
		return visitor.visitNode(this);
	}
	
	public void addColumn(Path item) {
		if (!containsColumn(item.toString()))
			columns.add(item);
	}
	
	public boolean containsColumn(String c) {
		for (Path col : columns) {
			if (col.toString().equals(c)) {
				return true;
			}
		}
		return false;
	}
	
	public PlanNode getChild() {
		return child;
	}
	

	public void setChild(PlanNode child) {
		this.child = child;
		this.child.setFather(this);
	}


	public List<Path> getColumns() {
		return columns;
	}
	
	public Path getColumn(int i) {
		return columns.get(i);
	}
	
	public void setColumns(List<Path> columns) {
		this.columns = columns;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Projection[" + this.getName() + "]: ");
		for (Path col: columns) {
			sb.append(col);
			sb.append(", ");
		}
		return sb.toString();
	}


	@Override
	public <T> void visitChildren(Visitor<T> visitor) {
		child.accept(visitor);
	}

	@Override
	public boolean containsRelation(String name) {
		if (this.name.equals(name))
			return true;
		return child.containsRelation(name);
	}
}
